import type { EntityManager } from '@mikro-orm/core';
import { Seeder, faker } from '@mikro-orm/seeder';
import * as Mock from 'mockjs' 
import { UsersFactory } from '../factorys/UsersFactory';
import { SourcesFactory } from '../factorys/SourcesFactory';
import { JobTasksFactory } from '../factorys/JobTasksFactory';
import { JobFactory } from '../factorys/JobsFactory';
import { EnrollFactory } from '../factorys/EnrollFactory';

const Random = Mock.Random

export class DatabaseSeeder extends Seeder {

  async run(em: EntityManager): Promise<void> {

    const users = await new UsersFactory(em).each((user)=>{
      user.sourcesAsCreator.set(
        new SourcesFactory(em) //创建项目
        .each((source)=>{
            source.tasks.set(
              new JobTasksFactory(em) //创建任务
              .each((task)=>{
                  task.isPublic = true
                  task.title = source.title
                  task.jobs.set(new JobFactory(em) //创建岗位
                  .each((job)=>{
                      job.enrolls.set(new EnrollFactory(em)
                      .each((enroll)=>{
                      }).make(5,{
                        jobTask:task
                      }))
                  })
                  .make(4,{
                     task:task
                  })) 
              })
              .make(3,{
                source,
                creator:user,
                owner:user,
              })
            )
        })
        .make(3,{
          creator:user,
          owner:user,
        })
      )
    }).create(10,{
      sex: faker.name.sex(),
      modules: '[]'
    })

    await em.persistAndFlush(users)

    // users.p

    // em.create(Users, {
    //   name: Random.cname(),
    // });
  }
}
